package cn.iocoder.yudao.module.erp.controller.admin.invoicechecklistitem;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.invoice.vo.InvoiceRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.invoicechecklistitem.vo.InvoiceCheckListItemPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.invoicechecklistitem.vo.InvoiceCheckListItemRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.invoicechecklistitem.vo.InvoiceCheckListItemSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.invoice.InvoiceDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.invoicechecklistitem.InvoiceCheckListItemDO;
import cn.iocoder.yudao.module.erp.service.invoicechecklistitem.InvoiceCheckListItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP 采购发票审核单项")
@RestController
@RequestMapping("/erp/invoice-check-list-item")
@Validated
public class InvoiceCheckListItemController {

    @Resource
    private InvoiceCheckListItemService invoiceCheckListItemService;



    @PostMapping("/create")
    @Operation(summary = "创建ERP 采购发票审核单项")
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list-item:create')")
    public CommonResult<Long> createInvoiceCheckListItem(@Valid @RequestBody InvoiceCheckListItemSaveReqVO createReqVO) {
        return success(invoiceCheckListItemService.createInvoiceCheckListItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 采购发票审核单项")
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list-item:update')")
    public CommonResult<Boolean> updateInvoiceCheckListItem(@Valid @RequestBody InvoiceCheckListItemSaveReqVO updateReqVO) {
        invoiceCheckListItemService.updateInvoiceCheckListItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 采购发票审核单项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list-item:delete')")
    public CommonResult<Boolean> deleteInvoiceCheckListItem(@RequestParam("id") Long id) {
        invoiceCheckListItemService.deleteInvoiceCheckListItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 采购发票审核单项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list-item:query')")
    public CommonResult<InvoiceCheckListItemRespVO> getInvoiceCheckListItem(@RequestParam("id") Long id) {
        InvoiceCheckListItemDO invoiceCheckListItem = invoiceCheckListItemService.getInvoiceCheckListItem(id);
        return success(BeanUtils.toBean(invoiceCheckListItem, InvoiceCheckListItemRespVO.class));
    }

    @GetMapping("/getInvoiceCheckListItemByNumbers")
    @Operation(summary = "获得ERP 采购发票审核单项")
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list-item:query')")
    public CommonResult<List<InvoiceRespVO>> getInvoiceCheckListItemByNumbers(@Valid InvoiceCheckListItemPageReqVO pageReqVO) {
        List<InvoiceDO> invoiceDOS = invoiceCheckListItemService.getInvoiceCheckListItemByNumbers(pageReqVO.getNumbers());
        return success(BeanUtils.toBean(invoiceDOS, InvoiceRespVO.class));
    }

    @GetMapping("/getNumAndPrice")
    @Operation(summary = "获得ERP 采购发票审核单项")
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list-item:query')")
    public CommonResult<InvoiceRespVO> getNumAndPrice(@Valid InvoiceCheckListItemPageReqVO pageReqVO) {
        InvoiceDO invoiceDO = invoiceCheckListItemService.getNumAndPrice(pageReqVO.getNumbers());
        return success(BeanUtils.toBean(invoiceDO, InvoiceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 采购发票审核单项分页")
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list-item:query')")
    public CommonResult<PageResult<InvoiceCheckListItemRespVO>> getInvoiceCheckListItemPage(@Valid InvoiceCheckListItemPageReqVO pageReqVO) {
        PageResult<InvoiceCheckListItemDO> pageResult = invoiceCheckListItemService.getInvoiceCheckListItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InvoiceCheckListItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 采购发票审核单项 Excel")
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list-item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInvoiceCheckListItemExcel(@Valid InvoiceCheckListItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InvoiceCheckListItemDO> list = invoiceCheckListItemService.getInvoiceCheckListItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 采购发票审核单项.xls", "数据", InvoiceCheckListItemRespVO.class,
                        BeanUtils.toBean(list, InvoiceCheckListItemRespVO.class));
    }

}